package frontend.components.base;

import frontend.components.other.UserInputCallableAction;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.request.resource.ContextRelativeResource;

public class TextFieldAndButtonComponent <ModelType> extends Panel {

    public TextFieldAndButtonComponent(String propertyId, final String labelResourceKey, ModelType model,
                                       final UserInputCallableAction action) {
        super(propertyId);
        Label label = new Label("label", new ResourceModel(labelResourceKey));
        final TextField<String> textField = new TextField<String>("textField",
                new PropertyModel<String>(model, propertyId));
        textField.setOutputMarkupId(true);

        AjaxLink link = new AjaxLink("link") {

            @Override
            public void onClick(AjaxRequestTarget target) {
                action.call(target, null);
                target.add(textField);
            }
        };
        link.add(new Image("linkImg", new ContextRelativeResource("images/icon_set/16/Database.png")));
        link.add(AttributeModifier.replace("title", new ResourceModel(labelResourceKey + ".tooltip")));
        add(label, textField, link);
        setOutputMarkupId(true);
        setOutputMarkupPlaceholderTag(true);
    }
}
